﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tosi
{

    class Junction
    {
        public List<Street> inStreets;
        public List<Street> outStreets;
        private int max;
        private int min;
        private int maxCars;
        private int greenLightIndex = 0;
        private int t = 0;

        public Junction()
        {
            inStreets = new List<Street>();
            outStreets = new List<Street>();
            greenLightIndex = 0;
        }
        public Street getDirection()
        {
            List<int> indexList = new List<int>();
            for (int i = 0; i < outStreets.Count; i++)
            {
                indexList.Add(i);
            }
            while (indexList.Count != 0)
            {
                int dir = indexList[Var.varRandom.getRandomNr(0, indexList.Count - 1)];
                indexList.Remove(dir);
                if (outStreets[dir].cars.Count == 0)
                    return outStreets[dir];
                Point lastCarPos = new Point(0, 0);
                lastCarPos.setTo(outStreets[dir].cars[outStreets[dir].cars.Count - 1].getCarPos());
                Point lastPossiblePos = new Point(0, 0);
                lastPossiblePos.setX(lastCarPos.getX() - Var.CarOffset * outStreets[dir].getCosPhi());
                lastPossiblePos.setY(lastCarPos.getY() - Var.CarOffset * outStreets[dir].getSinPhi());
                if ((outStreets[dir].getStart() < lastPossiblePos) * (outStreets[dir].getStart() < outStreets[dir].getEnd()) == 1)
                    return outStreets[dir];
            }
            return null;
        }
        public void addInStreet(ref Street inStr)
        {
            inStreets.Add(inStr);
        }
        public void addOutStreet(ref Street outStr)
        {
            outStreets.Add(outStr);
        }
        public List<Street> getInStreets()
        {
            return inStreets;
        }
        public void setMax() {
            max = inStreets[0].getMyLight().getPriority();
            t = 0;
            inStreets[0].getMyLight().incTime();
            //Console.Write("({0:D2}, {1:D5}) ", inStreets[0].getMyLight().getId(), inStreets[0].getMyLight().getPriority());
            for (int i = 1; i<inStreets.Count; i++) {
                //Console.Write("{0}\t | ", inStreets[i].getMyLight().getPriority());
                //Console.Write("({0:D2}, {1:D5}) ", inStreets[i].getMyLight().getId(), inStreets[i].getMyLight().getPriority());
                inStreets[i].getMyLight().incTime();
                if (inStreets[i].getMyLight().getPriority() > max)
                {
                    t = i;
                    max = inStreets[i].getMyLight().getPriority();
                }
            }
            //Console.WriteLine("~ {0:D2}->{1:D2}", greenLightIndex, t);
        }
        public void setMin()
        {
            min = inStreets[0].getMyLight().getPriority();
            for (int i = 1; i < inStreets.Count; i++)
            {
                if (inStreets[i].getMyLight().getPriority() < min) min = inStreets[i].getMyLight().getPriority();
            }
        }
        public int setLights()
        {
            //Console.WriteLine(" {0}",inStreets.Count);
            for (int i = 0; i < inStreets.Count; i++)
            {
                inStreets[i].myLight.setPriority();
            }
            //resetPriorities();
            setMax();
            if (t != greenLightIndex)
            {
                inStreets[greenLightIndex].myLight.setLight(1);
                inStreets[t].myLight.setLight(0);
                greenLightIndex = t;
            }
            //Console.ReadKey();
            return inStreets.Count;
        }
        public void resetPriorities() {
            //Console.Write("ID:{0}  ", id);
            setMax();
            //Console.WriteLine();
            //setMin();
            if (max > 20011) {
                for (int i = 0; i < inStreets.Count; i++) {
                    inStreets[i].getMyLight().resetPriority();
                }
            }
        }
        public void initLight()
        {
            inStreets[greenLightIndex].getMyLight().setLight(0);
        }
        public void setMaxCars()
        {
            maxCars = 0;
            for (int i = 0; i < inStreets.Count; i++)
            {
                maxCars += inStreets[i].getMaxCars();
            }
        }
        public int getMaxCars()
        {
            return maxCars;
        }
    }
  
}
